import vue from '@vitejs/plugin-vue'
import {defineConfig} from 'vite'

import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import {TDesignResolver} from 'unplugin-vue-components/resolvers'

import vueJsx from '@vitejs/plugin-vue-jsx'

import path from 'path'


export default defineConfig(({command, mode}) => {
    return {
        // 项目插件
        plugins: [
            vue(),
            vueJsx(),
            AutoImport({
                imports: [
                    'vue',
                    'vue/macros',
                    'vue-router',
                    '@vueuse/core',
                    'pinia',
                ],
                resolvers: [TDesignResolver({
                    library: 'vue-next',
                })],
                // 生成的文件目录
                dts: 'types/auto-imports.d.ts',
                // 配置本地需要自动导入的库
                dirs: [
                    // pinia状态管理目录
                    'src/stores',
                    // 自定义组合式api目录
                    'src/composables'
                ]
            }),
            Components({
                resolvers: [TDesignResolver({
                    library: "vue-next"
                })],
                // 生成类型的地址
                dts: 'types/components.d.ts',
                dirs: ['src/components']
            })
        ],
        // 基础配置
        base: './',
        publicDir: 'public',
        resolve: {
            alias: {
                '@': path.resolve(__dirname, 'src'),
            },
        },
        css: {
            preprocessorOptions: {
                less: {
                    modifyVars: {
                        '@border-color-base': '#dce3e8',
                    },
                    javascriptEnabled: true,
                },
            },
        },
        build: {
            outDir: 'dist',
            assetsDir: 'assets',
            assetsInlineLimit: 4096,
            cssCodeSplit: true,
            brotliSize: false,
            sourcemap: false,
            minify: 'terser',
            terserOptions: {
                compress: {
                    // 生产环境去除console及debug
                    drop_console: false,
                    drop_debugger: true,
                },
            },
            chunkSizeWarningLimit:5000,
            rollupOptions:{
                output:{
                    manualChunks(id){
                        if (id.includes('node_modules')) {
                            return 'vendor'; // 将第三方库打包进 vendor.js
                        }
                        if (id.includes('some-large-module')) {
                            return 'large-module'; // 自定义拆分某个大模块
                        }
                    }
                }
            }
        },
    }
})


